﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.IO;
using Newtonsoft.Json;
namespace WXFramework.Model
{
    /// <summary>
    /// 微信消息基类
    /// </summary>
    [Serializable]
   public abstract class RequestMessage:WXMessage
    {
        public RequestMessage()
        {

        }

        public RequestMessage(XElement xml)
        {
            this.FromUserName = xml.Element("FromUserName").Value;
            this.ToUserName = xml.Element("ToUserName").Value;
            this.CreateTime = Int64.Parse(xml.Element("CreateTime").Value);
            this.MsgId = xml.Element("MsgId") != null ? Int64.Parse(xml.Element("MsgId").Value) : 0;
            this.AgentID = xml.Element("AgentID") != null ? int.Parse(xml.Element("AgentID").Value) : -1;
        }

        public static T Deserializ<T>(Stream stream)
            where T : RequestMessage
        {
            var xs = new XmlSerializer(typeof(T));
            return (T)xs.Deserialize(stream);
        }

        [XmlElement("MsgId")]
        public long MsgId { get; set; }

        
        /// <summary>
        /// 是不是企业号
        /// </summary>
        [XmlIgnore]
        [JsonIgnore]
        public bool IsCorp 
        {
            get
            {
                return this.AgentID != -1;
            }
        }
    }
}
